﻿namespace Hidistro.UI.Web.Admin
{
    using Hidistro.ControlPanel.Store;
    using Hidistro.Core;
    using Hidistro.Membership.Context;
    using Hidistro.UI.Common.Controls;
    using Hidistro.UI.ControlPanel.utility;
    using Hishop.Components.Validation;
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.IO;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    [AdministerCheck(true)]
    public class Etaoset : AdminPage
    {
        protected Button btnChangeEmailSettings;
        protected Button btnUpoad;
        protected FileUpload fudVerifyFile;
        protected HtmlGenericControl fulDir;
        protected HtmlGenericControl incDir;
        protected Label lblEtaoFeedFull;
        protected Label lblEtaoFeedInc;
        protected YesNoRadioButtonList rdobltIsCreateFeed;
        protected TextBox txtEtaoID;

        protected void btnChangeEmailSettings_Click(object sender, EventArgs e)
        {
            SiteSettings masterSettings = SettingsManager.GetMasterSettings(false);
            masterSettings.EtaoID = this.txtEtaoID.Text;
            masterSettings.IsCreateFeed = this.rdobltIsCreateFeed.SelectedValue;
            if (this.ValidationSettings(masterSettings))
            {
                SettingsManager.Save(masterSettings);
                this.LoadEtaoOpen();
                this.ShowMsg("保存成功。", true);
            }
        }

        protected void btnUpoad_Click(object sender, EventArgs e)
        {
            if (this.fudVerifyFile.HasFile)
            {
                if (this.fudVerifyFile.PostedFile.ContentType.ToLower(CultureInfo.InvariantCulture) != "text/plain")
                {
                    this.ShowMsg("只能上传TXT文本文件", false);
                }
                else if (!this.fudVerifyFile.FileName.ToLower(CultureInfo.InvariantCulture).EndsWith(".txt") || (this.fudVerifyFile.FileName.IndexOf('.') != (this.fudVerifyFile.FileName.Length - 4)))
                {
                    this.ShowMsg("文件名只能有一个.号", false);
                }
                else if (this.fudVerifyFile.FileName.ToLower() != "etao_domain_verify.txt")
                {
                    this.ShowMsg("你上传的不是一淘的验证文件!", false);
                }
                else
                {
                    string str = "etao_domain_verify.txt";
                    string applicationPath = string.Empty;
                    string filename = string.Empty;
                    if (!string.IsNullOrEmpty(Globals.ApplicationPath))
                    {
                        if (Globals.ApplicationPath.EndsWith(@"\"))
                        {
                            applicationPath = Globals.ApplicationPath;
                        }
                        else
                        {
                            applicationPath = Globals.ApplicationPath + @"\";
                        }
                        filename = HiContext.Current.Context.Request.MapPath(applicationPath + str);
                    }
                    else
                    {
                        filename = HiContext.Current.Context.Request.MapPath("/");
                        if (filename.EndsWith(@"\"))
                        {
                            filename = filename + str;
                        }
                        else
                        {
                            filename = filename + @"\" + str;
                        }
                    }
                    this.fudVerifyFile.SaveAs(filename);
                    this.ShowMsg("上传成功。", true);
                }
            }
            else
            {
                this.ShowMsg("需要选择验证文件再点击上传。", false);
            }
        }

        protected void LoadEtaoOpen()
        {
            SiteSettings masterSettings = SettingsManager.GetMasterSettings(false);
            this.txtEtaoID.Text = masterSettings.EtaoID;
            this.rdobltIsCreateFeed.SelectedValue = masterSettings.IsCreateFeed;
            if (masterSettings.IsCreateFeed)
            {
                if (File.Exists(this.Page.Server.MapPath(Globals.ApplicationPath + "/storage/Root/IncrementIndex.xml")))
                {
                    this.lblEtaoFeedInc.Text = HiContext.Current.HostPath + Globals.ApplicationPath + "/storage/Root/IncrementIndex.xml";
                    this.incDir.Visible = true;
                }
                else
                {
                    this.incDir.Visible = false;
                }
                if (File.Exists(this.Page.Server.MapPath(Globals.ApplicationPath + "/storage/Root/FullIndex.xml")))
                {
                    this.lblEtaoFeedFull.Text = HiContext.Current.HostPath + Globals.ApplicationPath + "/storage/Root/FullIndex.xml";
                    this.fulDir.Visible = true;
                }
                else
                {
                    this.fulDir.Visible = false;
                }
            }
            else
            {
                this.incDir.Visible = false;
                this.fulDir.Visible = false;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                this.LoadEtaoOpen();
            }
        }

        private bool ValidationSettings(SiteSettings setting)
        {
            ValidationResults results = Hishop.Components.Validation.Validation.Validate<SiteSettings>(setting, new string[] { "ValMasterSettings" });
            string msg = string.Empty;
            if (!results.IsValid)
            {
                foreach (ValidationResult result in (IEnumerable<ValidationResult>) results)
                {
                    msg = msg + Formatter.FormatErrorMessage(result.Message);
                }
                this.ShowMsg(msg, false);
            }
            return results.IsValid;
        }
    }
}

